<?php
class UsersDetail extends Model {
	function UsersDetail(){
		parent::Model();
    }
    function getDetail($uid){
    	$result= array();
        $query = "SELECT * FROM users_detail WHERE user_id='$uid' LIMIT 1";
    	$query = $this->db->query($query);
    	if($query->result()){
    		foreach ($query->result() as $row){
  				$result ['tglLahir']= date("j F, Y",strtotime($row->birthdate));
  				$result ['about_me']= $row->about_me;
  				$result ['gender']= $row->gender;
  				$result ['location']= $row->location;
  				$result ['reg_date']= date("j F, Y",strtotime($row->reg_date));
  				$result ['avatar']= $row->avatar;
  				$result ['website']= $row->website;
  				$result ['work_phone']= $row->work_phone;
  				$result ['personal_phone']= $row->personal_phone;
  				$result ['im_skype']= $row->im_skype;
  				$result ['im_gtalk']= $row->im_gtalk;
  				$result ['im_msn']= $row->im_msn;
  				$result ['im_yahoo']= $row->im_yahoo;
  				$result ['im_aim']= $row->im_aim;
  				$result ['prof_facebook']= $row->prof_facebook;
  				$result ['prof_twitter']= $row->prof_twitter;
  				$result ['prof_flickr']= $row->prof_flickr;
  				$result ['prof_friendfeed']= $row->prof_friendfeed;
  				$result ['prof_myspace']= $row->prof_myspace;
  				$result ['prof_youtube']= $row->prof_youtube;
  				$result ['lastlogin_date'] = date("j F, Y",strtotime($row->lastlogin_date));
			}
			$result['ok'] = TRUE;
    	}else{
    		$result['ok'] = FALSE;
    	}
        return json_encode($result);
    }
    
 	function getUserIdFromUsername($username){
    	$result= array();
    	$query = "SELECT * FROM user WHERE username='$username' LIMIT 1";
    	$query = $this->db->query($query);
 		if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
				$uid  = $row->id;
			}
    	}
        return $uid;
    }
    
    
    function update($uid,
    				$tglLahir,
    				$about_me,
    				$gender,
    				$location){
    
    	$query = "UPDATE users_detail SET birthdate='$tglLahir',
    										about_me='$about_me',
    										gender='$gender',
    										location='$location' 
    										WHERE user_id='$uid'";
    	$query = $this->db->query($query);
    }
    
    function updateAvatar($uid,$avatar){
    	$query = "UPDATE users_detail SET avatar='$avatar' WHERE user_id='$uid'";
    	$query = $this->db->query($query);
    }
    
	function updateContact($uid,
    				$website,
    				$work_phone,
    				$personal_phone,
    				$im_skype,
    				$im_gtalk,
    				$im_msn,
    				$im_yahoo,
    				$im_aim,
    				$prof_facebook,
    				$prof_twitter,
    				$prof_flickr,
    				$prof_friendfeed,
    				$prof_myspace,
    				$prof_youtube){
    	$query = "UPDATE users_detail SET website='$website',
    										work_phone='$work_phone',
    										personal_phone='$personal_phone',
    										im_skype='$im_skype',
    										im_gtalk='$im_gtalk',
    										im_msn='$im_msn',
    										im_yahoo='$im_yahoo',
    										im_aim='$im_aim',
    										prof_facebook='$prof_facebook',
    										prof_twitter='$prof_twitter',
    										prof_flickr='$prof_flickr',
    										prof_friendfeed='$prof_friendfeed',
    										prof_myspace='$prof_myspace',
    										prof_youtube='$prof_youtube'
    										WHERE user_id='$uid'";
    	$query = $this->db->query($query);
    }
}